<?php
namespace app\common\search\tysj;

/**
 * 提供搜索条件
 */
class Product extends Base{
	protected $request = 'tysj\\Product';

	//公开列表搜索器
	public function list($model, $post = null) {
		if(!$post) {
			$post = $this->request->list();
		}

		//无条件默认添加搜索条件
		$this->list_default($model);

		//是否查询指定店铺的商品
		$this->list_sid($model, $post);

		//是否查询佣金
        $this->list_commission($model, $post);

        //获取产品分类下的产品
        $this->list_cid($model, $post);

        //是否追加最低价格字段
        $this->list_price($model, $post);

        //是否追加search查询
        $this->list_search($model, $post);

        //是否获取规格列表
        $this->list_specs_list($model, $post);

        //是否添加类型搜索条件
        $this->list_type($model, $post);

        //是否添加搜索团购条件
        $this->list_group_buying($model, $post);

        //是否添加搜索地区土特产条件
        $this->list_native_product($model, $post);
	}

	//私有列表搜索器
	public function private_list($model, $post = null) {
		if(!$post) {
			$post = $this->request->private_list();
		}

		//无条件默认添加搜索条件
		$this->list_default($model);

		//是否查询指定店铺的商品
		$this->list_sid($model, $post);

		//是否查询佣金
        $this->list_commission($model, $post);

        //获取产品分类下的产品
        $this->list_cid($model, $post);

        //是否追加最低价格字段
        $this->list_price($model, $post);

        //是否追加search查询
        $this->list_search($model, $post);

        //是否获取规格列表
        $this->list_specs_list($model, $post);

        //是否添加类型搜索条件
        $this->list_type($model, $post);

        //是否添加搜索团购条件
        $this->list_group_buying($model, $post);

        //是否添加搜索地区土特产条件
        $this->list_native_product($model, $post);

	}

	//无条件默认添加搜索条件
	public function list_default($model) {
		$statusEnum = loadEnum('ProductStatus');
		$model->alias('P')->join('store S', 'P.sid = S.id')->where(['S.status' => $statusEnum->showStatus()]);

		$sids = loadModel('StoreCurrentLimiting')->column('sid');
		$model->whereNotIn('P.sid', $sids);
		$model->order('odr desc,id desc');
		$model->field('P.*');
	}

	//是否查询指定店铺的商品
	public function list_sid($model, $post) {
		if($post['sid']) {
			$this->list_sid_ipl($model, $post);
		}
	}

	//是否查询指定店铺的商品直接实现
	public function list_sid_ipl($model, $post) {
		$key = $this->get_search_key($model, 'sid');
		$model->where($key, $post['sid']);
	}

	//是否查询佣金
	public function list_commission($model, $post) {
		if($post['commission']) {
            $this->list_commission_ipl($model, $post);
        }
	}

	//是否查询佣金不判断参数直接实现
	public function list_commission_ipl($model, $post) {
		$model->append(['commission']);
	}

	//是否添加产品分类
	public function list_cid($model, $post) {
		if($post['cid']) {
            $this->list_cid_ipl($model, $post);
        }
	}

	//是否添加产品分类直接实现
	public function list_cid_ipl($model, $post) {
		$key = $this->get_search_key($model, 'cid');
		$model->where($key, $post['cid']);
	}

	//是否追加最低价格字段
	public function list_price($model, $post) {
		if($post['price']) {
    		$this->list_price_ipl($model, $post);
    	}
	}

	//是否追加最低价格字段直接实现
	public function list_price_ipl($model, $post) {
		$model->append(['price']);
	}

	//是否追加搜索子数据
	public function list_search($model, $post) {
		if($post['search']) {
    		$this->list_search_ipl($model, $post);
    	}
	}

	//是否追加搜索直接实现
	public function list_search_ipl($model, $post) {
		$keys = ['name', 'selling'];
		foreach($keys as $k => $v) {
			$keys[$k] = $this->get_search_key($model, $v);
		}
		$keys = implode('|', $keys);
		$model->where($keys, '%' . $post['search'] . '%');
	}

	//是否获取规格列表
	public function list_specs_list($model, $post) {
		if($post['specs_list']) {
    		$this->list_specs_list_ipl($model, $post);
    	}
	}

	//是否获取规格列表直接实现
	public function list_specs_list_ipl($model, $post) {
		$model->append(['specs_list']);
	}

	//是否添加类型搜索条件
	public function list_type($model, $post) {
		if($post['type']) {
			$this->list_type_ipl($model, $post);
		}
	}

	//是否添加类型搜索条件直接实现
	public function list_type_ipl($model, $post) {
		$fun = 'list_type_' . $post['type'] . '_ipl';
		if(method_exists($this, $fun)) {
			$this->$fun($model, $post);
		}
	}

	//添加正在出售中的搜索条件
	public function list_type_sale_ipl($model, $post) {
		$key = $this->get_search_key($model, 'id');
		$statusEnum = loadEnum('ProductStatus');
		$ids = loadModel('tysj\\ProductSpecs')->alias('PS')->join('ProductSpecsNexus PSN', 'PS.id = PSN.psid')->where('inventory > 0')->column('PSN.pid');
		$model->show_list()->whereIn($key, $ids);
	}

	//添加库存预警的搜索条件
	public function list_inventory_warning_ipl($model, $post) {
		$key = $this->get_search_key($model, 'id');
		$ids = loadModel('tysj\\ProductSpecs')->alias('PS')->join('ProductSpecsNexus PSN', 'PS.id = PSN.psid')->where('inventory < inventory_warning and inventory > 0')->column('PSN.pid');
		$model->whereIn($key, $ids);
	}

	//添加售完商品的搜索条件
	public function list_sold_out_ipl($model, $post) {
		$key = $this->get_search_key($model, 'id');
		$ids = loadModel('tysj\\ProductSpecs')->alias('PS')->join('ProductSpecsNexus PSN', 'PS.id = PSN.psid')->where('inventory = 0')->column('PSN.pid');
    	$model->whereIn($key, $ids);
	}

	//添加下架商品的搜索条件
	public function list_hide_ipl($model, $post) {
		$key = $this->get_search_key($model, 'status');
		$model->where($key, loadEnum('ProductStatus')->hideStatus());
	}

	//是否添加搜索团购条件
	public function list_group_buying($model, $post) {
		if($post['is_group_buying']) {
			$this->list_group_buying_ipl($model, $post);
		}
	}

	//添加搜索团购条件
	public function list_group_buying_ipl($model, $post) {
		$model->join('product_group_buying PGB', 'P.id = PGB.pid');
		$model->where('PGB.id > 0');
	}

	//是否添加搜索地区土特产条件
	public function list_native_product($model, $post) {
		if($post['area_id']) {
			$this->list_native_product_ipl($model, $post);
		}
	}

	//添加搜索土特产地区条件
	public function list_native_product_ipl($model, $post) {
		$model->join('native_product NP', 'P.id = NP.pid');
		$model->where('NP.areaid', $post['area_id']);
	}
}
